package com.tanhua.app.service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.api.FriendApi;
import com.tanhua.api.UserApi;
import com.tanhua.api.UserInfoApi;
import com.tanhua.app.exception.BusinessException;
import com.tanhua.app.intercept.UserHolder;
import com.tanhua.commons.utils.Constants;
import com.tanhua.config.template.HuanXinTemplate;
import com.tanhua.model.pojo.User;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class MessageService {

    @DubboReference
    private UserApi userApi;

    @DubboReference
    private UserInfoApi userInfoApi;

    @DubboReference
    private FriendApi friendApi;

    @Autowired
    private HuanXinTemplate huanXinTemplate;

    /**
     * 根据环信id查询用户详细信息
     * @return
     */
    public UserInfoVo findUserInfoByhuanxinId(String huanxinId) {
        //1.根据用户环信id查询用户信息
        User user =  userApi.findByHuanXinId(huanxinId);
        //2.更具用户id查询用户详情
        UserInfo userInfo = userInfoApi.findById(user.getId());

        //3.创建UserInfoVo对象封装用户详情
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo, userInfoVo);//copy同名同类型的属性
        //UserInfo的字段名属性和前端数据属性不一致时
        if (userInfo.getAge() != null){
            userInfoVo.setAge(userInfo.getAge().toString());
        }
        return userInfoVo;
    }


    /**
     * 添加好友
     * @param friendId
     */
    public void contacts(Long friendId) {
        //1.将好友关系注册到环信
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + UserHolder.getUserId(),
                Constants.HX_USER_PREFIX + friendId);

        if (!aBoolean){
            //注册失败抛出异常
            throw new BusinessException(ErrorResult.huanXinError());
        }
        //2.注册成功，将好友信息存储到mongodb数据库的关系表中
        friendApi.saveFriend(UserHolder.getUserId(), friendId);
    }

    /**
     * 查询好友列表（分页查询）
     * @param page
     * @param pagesize
     * @param keyword
     * @return
     */
    public PageResult findFriends(Integer page, Integer pagesize, String keyword) {
        //1.调用Api查询当前用户全部好友的id,根据当前用户的id查询该用户的好友
        List<Long> friendIds = friendApi.findByUserId(UserHolder.getUserId());
        if (CollUtil.isEmpty(friendIds)){
            return new PageResult(page,pagesize,0,null);
        }

        //2.调用Api查询用户全部好友的详情
        List<UserInfo> userInfoList = userInfoApi.findByFriends(page,pagesize,friendIds,keyword);

        //3.构造Vos对象
        List<ContactVo> contactVos = new ArrayList<>();
        for (UserInfo userInfo : userInfoList) {
            ContactVo contactVo = ContactVo.init(userInfo);
            contactVos.add(contactVo);
        }
        //4.返回
        return new PageResult(page,pagesize,0,contactVos);
    }

    /**
     * 删除好友关系
     * @param likeUserId
     */
    public void removeContacts(Long likeUserId) {
        //1.删除环信中的好友关系
        Boolean aBoolean =huanXinTemplate.deleteContact(Constants.HX_USER_PREFIX+UserHolder.getUserId(), Constants.HX_USER_PREFIX+likeUserId);
        if (!aBoolean){
            throw new BusinessException(ErrorResult.huanXin2Error());
        }

        //2.删除成功，将好友信息存从mongodb数据库中删除
        friendApi.removeFriend(UserHolder.getUserId(), likeUserId);
    }
}
